{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "16617a73",
   "metadata": {},
   "source": [
    "# 字典的查询"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "991369b0",
   "metadata": {},
   "source": [
    "> 1.in 方法查询\n",
    "> 2.get 方法获取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ea07b0c7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu1801': 'ZiQi', 'stu1802': 'XiaoFeng', 'stu1803': 'LiuYu'}\n"
     ]
    }
   ],
   "source": [
    "info = {\n",
    "    \"stu1801\":\"ZiQi\",\n",
    "    \"stu1802\":\"XiaoFeng\",\n",
    "    \"stu1803\":\"LiuYu\"\n",
    "}\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "67649250",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查询 key 是否再字典当中 用 in \n",
    "\"stu1801\" in info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "38d098ef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ZiQi'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " #get 方法 获取key对应的value值\n",
    "info.get(\"stu1801\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "7d3bf1d6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ZiQi\n"
     ]
    }
   ],
   "source": [
    "# 终上所述\n",
    "if \"stu1801\" in info:\n",
    "    print(info.get(\"stu1801\"))\n",
    "else:\n",
    "    print(\"请再一次输入\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0a0deeab",
   "metadata": {},
   "source": [
    "## 字典的一般方法\n",
    "> 1.字典的循环遍历\n",
    "2.fromkeys()方法\n",
    "3.setdefault()方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "907df3f0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu1801': 'ZiQi', 'stu1802': 'XiaoFeng', 'stu1803': 'LiuYu'}\n"
     ]
    }
   ],
   "source": [
    "info = {\n",
    "    \"stu1801\":\"ZiQi\",\n",
    "    \"stu1802\":\"XiaoFeng\",\n",
    "    \"stu1803\":\"LiuYu\"\n",
    "}\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "41c247f5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1801 ZiQi\n",
      "1802 XiaoFeng\n",
      "1803 LiuYu\n"
     ]
    }
   ],
   "source": [
    "# 循环遍历字典\n",
    "# 给所有的值使用同样的操作\n",
    "stu_id_list = []\n",
    "for a,b in info.items():\n",
    "    print(a[3:],b)\n",
    "    stu_id_list.append(a[3:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "1a39ec3a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['1801', '1802', '1803']"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_id_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "94e4bb4f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu1801': 'ZiQi', 'stu1802': 'XiaoFeng', 'stu1803': 'LiuYu'}\n",
      "ZiQi\n",
      "XiaoFeng\n",
      "LiuYu\n"
     ]
    }
   ],
   "source": [
    "# 练习：把info的 value 目前的格式进行统一修改，修改的范式为： 姓 名 Zi Qi Xiao Feng\n",
    "info = {\n",
    "    \"stu1801\":\"ZiQi\",\n",
    "    \"stu1802\":\"XiaoFeng\",\n",
    "    \"stu1803\":\"LiuYu\"\n",
    "}\n",
    "print(info)\n",
    "stu_name_list = []\n",
    "for a,b in info.items():\n",
    "    print(b)\n",
    "    stu_name_list.append(b)\n",
    "\n",
    "# insert和占位符用过都不能，还在寻找怎么分开姓名的方法\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b2730267",
   "metadata": {},
   "source": [
    "## fromkeys()方法\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "89f5083f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function fromkeys:\n",
      "\n",
      "fromkeys(iterable, value=None, /) method of builtins.type instance\n",
      "    Create a new dictionary with keys from iterable and values set to value.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(info.fromkeys)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "bfdf6d4d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stud2101': 123456, 'stud2102': 123456, 'stud2103': 123456}"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 初始化统一值的字典信息\n",
    "学生初始密码={}\n",
    "学生初始密码.fromkeys([\"stud2101\",\"stud2102\",\"stud2103\"],123456)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2e0a04db",
   "metadata": {},
   "source": [
    "## setdefault()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "6a958d82",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function setdefault:\n",
      "\n",
      "setdefault(key, default=None, /) method of builtins.dict instance\n",
      "    Insert key with a value of default if key is not in the dictionary.\n",
      "    \n",
      "    Return the value for key if key is in the dictionary, else default.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(info.setdefault)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "2abfee56",
   "metadata": {},
   "outputs": [],
   "source": [
    "info.setdefault('stu1804')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "4dc580c0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'zhichao'"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info.setdefault('stu1805','zhichao')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "ca6f62f0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1801': 'ZiQi',\n",
       " 'stu1802': 'XiaoFeng',\n",
       " 'stu1803': 'LiuYu',\n",
       " 'stu1804': None,\n",
       " 'stu1805': 'zhichao'}"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ed2cf2c3",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
